<?php
class UpFile_ApiClient
	{
		private static $instance=null;
		private $data=null;
		private $url='';
		private $referer='http://copr.hucai.com/index.php?a=copyright.UpFile_ApiClient';
		private $auth='';
		private $authcode='';
		private $header='';
		
		public function __construct()
			{
				
			}
		private function setheader()
			{
				$header=1;
				if($header)
					{
						$this->header=array(
							'CLIENT_TOKEN'=>$this->auth,
							'CLIENT_CODE'=>$this->authcode
						);
					}
					else
						{
							return false;
						}
				return true;
				
			}
		private function setauth($auth)
			{
				if($auth)
					{
						$this->auth=$auth;
						$this->authcode=md5(substr(md5($auth),5,15).md5($auth));
					}
					else
						{
							return false;
						}
				return true;
			}
		//设置CURL参数，并获取数据	
		private function Api_Curl()
			{
				$time_out=300;
				$timeout=1000;
				$url='';
				$referer='';
				$curl='';
				if(!$this->url && !$this->data)
					{
						return false;
					}
					try
						{
							if(!$curl=curl_init())
								{
									throw new Exception('初始化失败！');
								}
								else
									{
										
									}
						}
						catch(Exception $e)
							{
								$e->getMessage();
							}
				
				curl_setopt($curl,CURLOPT_URL,$this->url);//要转送数据到的网址（调用方）
				curl_setopt($curl,CURLOPT_REFERER,$this->referer);//发送数据的网址（被调用方）
				curl_setopt($curl,CURLOPT_HTTPHEADER,$this->header);
				//curl_setopt($curl,CURLOPT_HEADER,'content-type:aplication/json;charset=utf-8');
				curl_setopt($curl,CURLOPT_POST,true);//推送数据的方式
				curl_setopt($curl,CURLOPT_POSTFIELDS,$this->data);//推送的数据
				curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$time_out);//连接服务器没有反应的等待时间
				curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);//接收数据是等待的时间
				
				$result_data=curl_exec($curl);//执行推送数据
				curl_close($curl);
				return $result_data;
			}
		//对外接口
		public function DoRequest($url,$auth,$data=null)
			{
				if($this->setauth($auth))
					{
						if($this->setheader())
							{
								$this->url=$url;
								//$this->data=$data;
								if(!empty($data))
									{
										if(is_array($data))
											{
												$datas='';
												foreach($data as $key => $value)
													{
														$datas.=$key.'='.urlencode($value).'&';//urlencode()
													}
													$datas=rtrim($datas,'&');
													$this->data=$datas;
												unset($datas);
											}
									}
								$result_data=$this->Api_Curl();
								return $result_data;
								/* $json=json_decode(trim($result_data,chr(239).chr(187).chr(191)),true);
								
								if(count($json)==200)
									{
										echo 'success';
										exit;
									}
									else
										{
											echo 'failed';
											exit;
										} */
								
								//var_dump(json_decode(trim($result_data,chr(239).chr(187).chr(191)),true));
								
								//var_dump($json);
								//;'chr(239).'@'.chr(187).'@'.chr(191); 去掉URF-8的BOM头
								///php_json_decode()该函数在编码的时候系统的编辑软件可能会自动加上BOM编码的头部所以要去掉，bom 编码的头部
							//	字符。
								/*  echo '<br/><br/>';
								echo '<center><div style="text-align:left;width:150px;">';
								$this->display_multi_array($json);
								echo '</div></center>';	 
								file_put_contents('D:\xampp\htdocs\upfile_apiclient.txt',$result_data);
								exit; */
								/* foreach($json as $key => $value)
									{
										if(!is_array($value))
											{
												echo '<h3>'.$value.'</h3>';
											}
									}
								
								for($i=0;$i<count($json['data']);$i++)
									{
										if($json['data'][$i])
											{
												echo '<h3>'.$json['data'][$i]['code'].'</h3><br/>';
											}
									} */
								
								
								//echo $result_data;
							}
					}
			}
		//类的析构函数
		public function __destruct()
			{
				
			}
		//循环输出多维数组的所有元素
		private function display_multi_array($array)
			{
				if(empty($array))
					{
						return false;
					}
					elseif(is_array($array))
						{
							$count=count($array);
							if($count>1)
								{
									foreach($array as $key => $value )
										{
											if(!is_array($value))
												{
													
													echo '<h3><span style="color:green;margin-right:15px;">'.$key.':</span>'.$value.'</h3>';
												}
												else
													{
														
														$this->display_multi_array($value);
													}
										}
								}
							
						}
						else
							{
								echo '<h3>'.$array.'</h3>';
							}
			}	
			
	
	}
	header('content-type:text/html;charset=utf-8');	
	ini_set('date.timezone','Asia/Shanghai');
	importClass('img');
	require_once $_SITE_PATH_ . "m/m_information.php";
	require_once $_SITE_PATH_ . "m/m_agreement.php";
	$m_agreement=new m_agreement();
	$m_information=new m_information();
	

	$m_information->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile FROM information where `flow`>=9 limit 0,200';
	$upfile_array=$m_information->getArray($m_information->query());

	//var_dump($upfile_array);	
	
	$json=json_encode($upfile_array);
	$auth='123456';
$data=array('data'=>$json,'apitype'=>'upfile','auth'=>urlencode($auth),'code'=>urlencode(md5(substr(md5($auth),5,15).md5($auth))));
//$data=$json;
//echo $json;
$url='http://copr.hucai.com/jieshou.php';
//具体实现的时候，把单例模式改成公共非单例模式实现。UpFile_ApiClient::GetInstance()->DoRequest($url,$auth,$data);
$api=new UpFile_ApiClient();
$result=$api->DoRequest($url,$auth,$data);
if(strtolower($result)=='success')
	{
		$count=count($upfile_array);
		echo $count;
		echo '<br/>';
		/* for($i=0;$i<$count;$i++)
			{
				$m_information->_ssql='update information set ts_flag="1" where `id`='.$upfile_array[$i]->id;
				$m_information->query();
			} */
		file_put_contents('E:\test1\mytask.txt',$result.time());
		echo '数据推送成功！'.$result;
	}
	else
		{
			echo '数据推送失败！'.$result;
			file_put_contents('E:\test1\mytask.txt',$result.time());
		}
	
	
	/* $time=time();
	$ts_type='SH';
	$ts_num=200;
	$start_flag='';
	$end_flag='';
	$count=count($upfile_array);
	if($count>1 && $count<=$ts_num)
		{
			$start_flag=$upfile_array[0]->id;
			$end_flag=$upfile_array[$count-1]->id;
		}
		elseif($count>$ts_num)
			{
				$order=ceil($count/$ts_num);
				
				for($i=0;$i<$order;$i++)
					{
						if($i==0)
							{
								$start_flag=$upfile_array[($i)*$ts_num]->id;
								$end_flag=$upfile_array[(($i+1)*$ts_num-1)]->id;
							}
							elseif($i==($order-1))
								{
									$start_flag=$upfile_array[($i)*$ts_num+1]->id;
									$end_flag=$upfile_array[$count-1]->id;
								}
								else
									{
										$start_flag=$upfile_array[($i)*$ts_num+1]->id;
										$end_flag=$upfile_array[($i+1)*$ts_num]->id;
									}
						$m_tasklist->_ssql='select id from tasklist where ts_type="'.$ts_type.'" and start_flag="'.$start_flag.'" and end_flag="'.$end_flag.'"';		
						
						echo $m_tasklist->_ssql;
						echo '<br/>';
						
						$arr=$m_tasklist->getArray($m_tasklist->query());
						
						if($arr[0]->id && !empty($arr))
							{
									
							}
							else
								{
									$m_tasklist->_ssql='insert into tasklist(ts_type,start_flag,end_flag,ts_time,ts_num) values("'.$ts_type.'","'.$start_flag.'","'.$end_flag.'",'.$time.','.$ts_num.')';
									$m_tasklist->query();
								}
						
					}
			}
	 */
	
	

?>